package com.augurit.agmobile.agwater5.drainage.common.adapter;

import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @PROJECT GZZHSW
 * @USER Augurit
 * @CREATE 2021/1/7 14:21
 */
public class SimpleFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private final List<String> mTitles;

    public SimpleFragmentStatePagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        this(fm, fragments, Collections.emptyList());
    }

    public SimpleFragmentStatePagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);
        mFragments.addAll(fragments);
        mTitles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    public void update(List<Fragment> fragments) {
        mFragments.clear();
        mFragments.addAll(fragments);
    }
}
